package wnhz.com.business.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import wnhz.com.business.R;
import wnhz.com.business.impl.ActionBarClickListener;

/**
 * 支持渐变的 actionBar
 * on 2016/12/28.
 */

public final class TranslucentActionBarW extends LinearLayout {

    private View layRoot;
    private View vStatusBar;
    private View layLeft;
    private View layRight;
    public TextView tvTitle;
    public TextView tvLeft;
    public TextView tvRight;
    private ImageView iconLeft;
    private ImageView iconRight;
    private EditText edt_seach;
    private RelativeLayout lay_actionbar_right;

    public GetData getDatas;
    public String namedata;

    public TranslucentActionBarW(Context context) {
        this(context, null);
    }

    public TranslucentActionBarW(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TranslucentActionBarW(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void init() {
        setOrientation(HORIZONTAL);
        View contentView = inflate(getContext(), R.layout.actionbar_trans_white, this);
        layRoot = contentView.findViewById(R.id.lay_transroot);
        vStatusBar = contentView.findViewById(R.id.v_statusbar);
        tvTitle = (TextView) contentView.findViewById(R.id.tv_actionbar_title);
        tvLeft = (TextView) contentView.findViewById(R.id.tv_actionbar_left);
        tvRight = (TextView) contentView.findViewById(R.id.tv_actionbar_right);
        iconLeft = (ImageView) contentView.findViewById(R.id.iv_actionbar_left);
        iconRight = (ImageView) contentView.findViewById(R.id.iv_actionbar_right);
        edt_seach = (EditText) contentView.findViewById(R.id.edt_seach);
        lay_actionbar_right = (RelativeLayout) contentView.findViewById(R.id.lay_actionbar_right);


        edt_seach.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                namedata = edt_seach.getText().toString();
//                0000mydata.setedtdata(getDatas);
//                getDatas.getedittext(namedata);
            }
        });


    }


    public  interface GetData{
        void getedittext(String data);
    }

    /**
     * 设置状态栏高度
     *
     * @param statusBarHeight
     */
    public void setStatusBarHeight(int statusBarHeight) {
        ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();
        params.height = statusBarHeight;
        vStatusBar.setLayoutParams(params);
    }

    /**
     * 设置是否需要渐变
     */
    public void setNeedTranslucent() {
        setNeedTranslucent(true, false);
    }

    /**
     * 设置是否需要渐变,并且隐藏标题
     *
     * @param translucent
     */
    public void setNeedTranslucent(boolean translucent, boolean titleInitVisibile) {
        if (translucent) {
            layRoot.setBackgroundDrawable(null);
        }
        if (!titleInitVisibile) {
            tvTitle.setVisibility(View.GONE);
            tvRight.setVisibility(View.GONE);
        }
    }

    /**
     * 设置标题
     *
     * @param strTitle
     */
    public void setTitle(String strTitle) {
        if (!TextUtils.isEmpty(strTitle)) {
            tvTitle.setText(strTitle);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
    }

    /**
     * 设置Right标题
     *
     * @param strRight
     */
    public void setRight(String strRight) {
        if (!TextUtils.isEmpty(strRight)) {
            tvRight.setText(strRight);
        } else {
            tvRight.setVisibility(View.GONE);
        }
    }


    /**
     * 设置数据
     *
     * @param strTitle
     * @param resIdLeft
     * @param strLeft
     * @param resIdRight
     * @param strRight
     * @param listener
     */
    public void setData(String strTitle, int resIdLeft, String strLeft, int resIdRight, String strRight, int resIdCenter, final ActionBarClickListener listener) {
        if (!TextUtils.isEmpty(strTitle)) {
            tvTitle.setText(strTitle);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(strLeft)) {
            tvLeft.setText(strLeft);
            tvLeft.setVisibility(View.VISIBLE);
        } else {
            tvLeft.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(strRight)) {
            tvRight.setText(strRight);
            tvRight.setVisibility(View.VISIBLE);
        } else {
            tvRight.setVisibility(View.GONE);
        }

        if (resIdLeft == 0) {
            iconLeft.setVisibility(View.GONE);
        } else {
            iconLeft.setBackgroundResource(resIdLeft);
            iconLeft.setVisibility(View.VISIBLE);
        }

        if (resIdRight == 0) {
            iconRight.setVisibility(View.GONE);
        } else if (resIdRight == 1) {
            lay_actionbar_right.setVisibility(View.GONE);
        } else {
            iconRight.setBackgroundResource(resIdRight);
            iconRight.setVisibility(View.VISIBLE);
        }

        //搜索输入框
        if (resIdCenter == 0) {
            edt_seach.setVisibility(View.GONE);
        } else if (resIdCenter == 1) {
            edt_seach.setFocusable(false);
            edt_seach.setVisibility(View.VISIBLE);
            edt_seach.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onEditTextClick();
                }
            });
        } else {
            edt_seach.setVisibility(View.VISIBLE);
            edt_seach.setFocusable(true);
        }

        if (listener != null) {
            layLeft = findViewById(R.id.lay_actionbar_left);
            layRight = findViewById(R.id.lay_actionbar_right);
            layLeft.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onLeftClick();
                }
            });
            layRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onRightClick();
                }
            });
        }
    }
}
